package com.itheima.em.util;

import org.osgeo.proj4j.*;

/**
 *  WGS84 和 CGCS2000 的坐标系信息 转换
 */
public class CoordinateConverter {
    private static CoordinateReferenceSystem wgs84;
    private static CoordinateReferenceSystem cgcs2000;

    static {
        // 定义 WGS84 和 CGCS2000 的坐标系信息
        CRSFactory crsFactory = new CRSFactory();
        wgs84 = crsFactory.createFromName("EPSG:4326"); // WGS84 坐标系
        cgcs2000 = crsFactory.createFromName("EPSG:4490"); // CGCS2000 坐标系
    }

    public static double[] convertWGS84ToCGCS2000(double lon, double lat) {
        ProjCoordinate wgs84Coord = new ProjCoordinate(lon, lat);
        ProjCoordinate cgcs2000Coord = new ProjCoordinate();
        CoordinateTransform transform = new BasicCoordinateTransform(wgs84, cgcs2000);
        transform.transform(wgs84Coord, cgcs2000Coord);
        return new double[]{cgcs2000Coord.x, cgcs2000Coord.y};
    }

    public static double[] convertCGCS2000ToWGS84(double lon, double lat) {
        ProjCoordinate cgcs2000Coord = new ProjCoordinate(lon, lat);
        ProjCoordinate wgs84Coord = new ProjCoordinate();
        CoordinateTransform transform = new BasicCoordinateTransform(cgcs2000, wgs84);
        transform.transform(cgcs2000Coord, wgs84Coord);
        return new double[]{wgs84Coord.x, wgs84Coord.y};
    }
}
